package _12_LianXi;

/**
 * 12、 编写程序，实现3个窗口同时卖票，但是票不重复。(线程同步)
 * 
 * @author lenovo
 * @date 2020年7月24日
 */
public class Ticket {
	public static void main(String[] args) {
		MaiPiao mp = new MaiPiao(100);
		Thread t1 = new Thread(new Ticket1(mp));
		Thread t2 = new Thread(new Ticket1(mp));
		Thread t3 = new Thread(new Ticket1(mp));
		t1.setName("T1窗口");
		t2.setName("T2窗口");
		t3.setName("T3窗口");
		t1.start();
		t2.start();
		t3.start();

	}
}

class Ticket1 implements Runnable {
	MaiPiao mp;

	public Ticket1(MaiPiao mp) {
		super();
		this.mp = mp;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			synchronized ("123") {
				if (mp.piaoShu <= 0) {
					break;
				}
				try {
					Thread.sleep(30);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.shouPiao(mp.piaoShu--);
			}
		}
	}
}

class MaiPiao {
	int piaoShu;

	public MaiPiao(int piaoShu) {
		super();
		this.piaoShu = piaoShu;
	}

	public void shouPiao(int piaoShu) {
		System.out.println(Thread.currentThread().getName() + "卖出了第 : " + piaoShu + " 张票. 剩余 : " + (piaoShu - 1));
	}
}